package com.dawnling.mvvmdemo.demo1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.dawnling.mvvmdemo.R;
import com.dawnling.mvvmdemo.databinding.ActivityMainBinding;
import com.dawnling.mvvmdemo.demo2.MvvmActivity;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private Model model;
    private Model1 model1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        model = new Model("账户等级", 100);
        model1 = new Model1("账户等级", 100);
        //初始化account todo 记得初始化，否则赋值无效
        binding.setAccount(model);
        binding.setAccount1(model1);
        //初始化activity todo 记得初始化，否则点击事件无效
        binding.setActivity(this);
    }

    /**
     * 方法1 每次都得binding.setAccount
     *
     * @param v
     */
    public void onClick(View v) {
//        Toast.makeText(this, "点击了" ,Toast.LENGTH_SHORT).show();
        model.setLevel(model.getLevel() + 1);
        binding.setAccount(model);
    }

    /**
     * 方法2 省掉了每次都binding.setAccount  在Model做文章，继承BaseObservable
     *
     * @param v
     */
    public void onClick11(View v) {
        model1.setLevel(model1.getLevel() + 1);
    }

    public void onClick2(View v) {
        Intent intent = new Intent(this, MvvmActivity.class);
        startActivity(intent);
    }
}
